package com.jackiechancinema.ssm.service.Impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jackiechancinema.ssm.bo.ContractBo;
import com.jackiechancinema.ssm.bo.EmpBo;
import com.jackiechancinema.ssm.config.SystemContant;
import com.jackiechancinema.ssm.dao.ContractDao;
import com.jackiechancinema.ssm.dao.EmpDao;
import com.jackiechancinema.ssm.dto.ContractExcelDto;
import com.jackiechancinema.ssm.dto.CustomerDto;
import com.jackiechancinema.ssm.dto.EmpExcelDto;
import com.jackiechancinema.ssm.entity.Contract;
import com.jackiechancinema.ssm.entity.Customer;
import com.jackiechancinema.ssm.entity.Emp;
import com.jackiechancinema.ssm.listener.ContractReadListener;
import com.jackiechancinema.ssm.listener.EmpReadListener;
import com.jackiechancinema.ssm.service.Inte.ContractService;
import com.jackiechancinema.ssm.utils.BeanUtil;
import com.jackiechancinema.ssm.vo.PageVo;
import com.jackiechancinema.ssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Service
public class ContractServiceImpl implements ContractService {
    @Autowired
    private ContractDao contractDao;
    @Autowired
    private ContractReadListener listener;

    //新增
    @Override
    public R insert(Contract contract) {
        if (contractDao.insert(contract)>0){
            return R.ok(null);
        }else{
            return R.fail();
        }

    }
    //查询
    @Override
    public PageVo<Contract> queryPage(ContractBo contractBo) {
        if (contractBo != null){
            Contract contract = BeanUtil.copyProperty(Contract.class, contractBo, contractBo.getClass().getDeclaredFields());
            PageHelper.startPage(contractBo.getPage(), contractBo.getLimit());//开启分页
            PageInfo<Contract> pageInfo = new PageInfo<Contract>(contractDao.selectAll(contract));
            return new PageVo<Contract>(SystemContant.PAGE_R,"OK",pageInfo.getTotal(),pageInfo.getList());
        }
        return new PageVo<Contract>(SystemContant.PAGE_R,"OK",0,null);
    }
    //删除
    @Override
    public R deleteById(int id) {
        if (contractDao.delete(id)>0){
            return R.ok(null);
        }else{
            return R.fail();
        }
    }
    //批量删除
    @Override
    public R deleteBatch(List<Contract> contracts) {

        List<Integer> list = new ArrayList<>();
        for (Contract contract : contracts) {
            list.add(contract.getId());
        }

        if (contractDao.batchDel(list)>0){
            return R.ok(null);
        }else{
            return R.fail();
        }
    }
    @Override
    public R update(Contract contract) {
        if (contractDao.update(contract)>0){
            return R.ok(null);
        }else{
            return R.fail();
        }
    }

    @Override
    public R insertBatch(MultipartFile file) {
        if (!file.isEmpty()){
            try {
                EasyExcel.read(file.getInputStream(), ContractExcelDto.class,listener).sheet().doRead();
                return R.ok("OK");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return R.fail();
    }

    @Override
    public void exportExcel(ContractBo bo, HttpServletResponse response) {
        //1.设置响应消息头，目的：告诉请求方，这个是下载
        String fn=System.currentTimeMillis()+".xlsx";
        //设置响应头  告知浏览器，要以附件的形式保存内容   filename=浏览器显示的下载文件名
        response.setHeader("content-disposition","attachment;filename="+fn);

        //2.组装查询条件
        Contract contract=new Contract();
        if(StringUtils.hasLength(bo.getCname())){
            contract.setCname(bo.getCname());
        }
        if(StringUtils.hasLength(bo.getPartya())){
            contract.setPartya(bo.getPartya());
        }
        if(StringUtils.hasLength(bo.getPartyb())){
            contract.setPartyb(bo.getPartyb());
        }
        if(bo.getDate() != null){
            contract.setDate(bo.getDate());
        }
        //3.获取要下载的数据
        List<Contract> list=contractDao.selectAll(contract);
        try {
            //4.生成Excel，并将数据写出到响应流中
            EasyExcel.write(response.getOutputStream(),Contract.class).sheet().doWrite(list);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
